package com.it.gmall.list.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.it.gmall.common.result.Result;
import com.it.gmall.list.service.ListService;
import com.it.gmall.model.list.Goods;
import com.it.gmall.model.list.SearchAttr;
import com.it.gmall.model.list.SearchParam;
import com.it.gmall.model.list.SearchResponseVo;
import io.swagger.models.auth.In;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

   /* @Autowired
    private TransportClient transportClient; // 低版本客户端-早期*/
    @Autowired
    private RestHighLevelClient restHighLevelClient; // 高版本 最近

    /*@Autowired
    private ElasticsearchTemplate elasticsearchTemplate; // 低版本客户端-早期*/
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate; // 高版本客户端 最近

    @Autowired
    private ListService listService;

    // 初始化商品结构到es库中
    @GetMapping("/createIndex")
    public Result createIndex(){
        // 创建索引
        elasticsearchRestTemplate.createIndex(Goods.class);
        // 创建映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    // 下架商品
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable long skuId){
        listService.cancelSale(skuId);
        return Result.ok();
    }

    // 上架商品
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable long skuId){
        listService.onSale(skuId);
        return Result.ok();
    }

    // 热度评分
    @GetMapping("/inner/incrHotScore/{skuId}/{score}")
    public void incrHotScore(@PathVariable long skuId, @PathVariable Integer score){
        listService.incrHotScore(skuId,score);
    }

    // 搜索商品
    @PostMapping("/inner/searchItem")
    public SearchResponseVo searchItem(@RequestBody SearchParam searchParam){
        return listService.searchItem(searchParam);
    }

}
